package com.tyk.bookstore.back.payment.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.payment.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.payment.manager.excel.model.AmountLogExcelVo;
import com.tyk.bookstore.back.payment.model.query.AmountLogAddQuery;
import com.tyk.bookstore.back.payment.model.query.AmountLogPageQuery;
import com.tyk.bookstore.back.payment.model.query.AmountLogUpdateQuery;
import com.tyk.bookstore.back.payment.model.vo.AmountLogPageVo;
import com.tyk.bookstore.back.payment.model.vo.AmountLogVo;
import com.tyk.bookstore.back.payment.service.AmountLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 系统余额变更日志
 */
@Tag(name = "支付管理/系统余额变更日志管理", description = "系统余额变更日志管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/payment/amount-log")
public class AmountLogController {

    private final AmountLogService amountLogService;
    private final ExcelManager<AmountLogExcelVo> excelManager;

    @Operation(summary = "查询系统余额变更日志详情", description = "查询系统余额变更日志详情")
    @SaCheckPermission("payment:amount-log:get")
    @GetMapping("{id}")
    public R<AmountLogVo> getById(
            @Schema(description = "系统余额变更日志ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(amountLogService.getById(id));
    }

    @Operation(summary = "分页查询系统余额变更日志", description = "分页查询系统余额变更日志")
    @SaCheckPermission("payment:amount-log:page")
    @GetMapping("page")
    public R<AmountLogPageVo> getPage(
            @Schema(description = "系统余额变更日志分页查询参数")
            @ParameterObject
            @Validated AmountLogPageQuery query
    ) {
        return R.ok(amountLogService.getPage(query));
    }

    @Operation(summary = "添加系统余额变更日志", description = "添加系统余额变更日志")
    @SaCheckPermission("payment:amount-log:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody AmountLogAddQuery query
    ) {
        amountLogService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新系统余额变更日志", description = "更新系统余额变更日志")
    @SaCheckPermission("payment:amount-log:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody AmountLogUpdateQuery query
    ) {
        amountLogService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除系统余额变更日志", description = "删除系统余额变更日志")
    @SaCheckPermission("payment:amount-log:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "系统余额变更日志ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        amountLogService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除系统余额变更日志", description = "批量删除系统余额变更日志")
    @SaCheckPermission("payment:amount-log:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "系统余额变更日志ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        amountLogService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("payment:amount-log:excel:download")
    @GetMapping("excel")
    public void downloadExcel(AmountLogPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                amountLogService.getAmountLogExcelVoList(query),
                ExcelConstant.AMOUNT_LOG_FILE_NAME,
                ExcelConstant.AMOUNT_LOG_SHEET,
                resp
        );
    }


}

